******************************
* Stata preamble
******************************
capture log close
set linesize 255
set mat 1500
set more off,perm
sysdir
mata: mata mlib index
set scheme plotplainblind
global G gstats tab
global S stats(mean sd min p1 q p99 max n)
			*************************
			* Program -- Winsorize
			*************************	
			capture program drop WinStd_tb
				program define WinStd_tb
				disp "Creating Winsorized (w_) and Standardized (z_) of the following:"
				while "`1'" != "" {
					qui  {
								capture drop w_`1'
								winsor `1', gen(w_`1') p(.01)
								lab var w_`1' "w_`1'"
								capture drop z_`1'
								egen z_`1' = std(w_`1')
								lab var z_`1' "z_`1'"
					}
					disp `"    `1'"'
					macro shift 
				}
				end
				

pwd


******************************
* Figure 1: Aggregate Share Evolution
******************************
use CountyShares_2008_2015_RegShocks, clear
		
	xtset stcou year
	xtdescribe
	tabstat  Sh_LenderType_*_nloan,  by(year) stat(mean) format(%5.3f)


	desc
		gen Sh_LenderType_23_nloan = Sh_LenderType_2_nloan + Sh_LenderType_3_nloan
		gen Sh_LenderType_7_nloan = Sh_LenderType_5_nloan + Sh_LenderType_6_nloan
		drop Sh_LenderType_5_nloan  Sh_LenderType_6_nloan Sh_LenderType_2_nloan Sh_LenderType_3_nloan
		tabstat  Sh_LenderType_*_nloan,  by(year) stat(mean) format(%5.3f)

	qui foreach t in 1 23 4 7 8 9 10 {
		capture {
			gen nloanMn_`t' = Sh_LenderType_`t'_nloan * nloan_stcou / 1000000
		}
	}
	gen nloanMn_agg = nloan_stcou/1000000
		collapse (sum) nloanMn* ,by(year)
		disp "*********************" _n "Millions of Loans" _n "*********************"
		list, ab(30)
	qui foreach t in 1 23 4 7 8 9 10  {
		capture {
			gen Sh_`t'_agg = nloanMn_`t' / nloanMn_agg
				replace Sh_`t'_agg = Sh_`t'_agg*100
		}
	}
			list year Sh_*, ab(30)
		
			local gr gs12
			global TXTopts placement(west) size(small)
			twoway line  Sh_23 Sh_4 Sh_8 Sh_1_ Sh_10 Sh_9 Sh_7  year, plotregion(margin(l+25 r+5)) ///
				 name(agg_trends, replace) ///
				ylabel("") xlabel(2008(1)2015) legend(off)  ///
				xtitle("") yscale(lstyle(none)) ///
				lcolor( `gr'  `gr' `gr' black orangebrown vermillion blue )  lpattern(l l l l l l l) ///
			text( 12.7 2007.95 "Large Banks 13%"  /*-3.97*/ 10.5 2007.95 "Intermediate Banks 11%"   6.1 2007.95 "Credit Unions 6%"   , $TXTopts color(`gr')) ///
					text( 8.5 2015.4  "8%" 5.5 2015.4  "6%"     , $TXTopts color(`gr')) ///
				text( 32.9326 2007.95 "Big4 33%"  12.0 2015.4  "12%"     , $TXTopts  color(black) ) ///
				text( 16.4 2007.95 "Small Banks 16%" 19.0 2015.4  "19%"     , $TXTopts  color(blue) ) ///
				text(   20.1 2007.95 "Shadow Banks 20%" 36.9 2015.4  "37%"  , $TXTopts  color(vermillion)) ///
				text(  1.2 2007.95 "Fintech 1%" 10 2015.4  "10%" , $TXTopts  color(orangebrown)) 


**********************************
* Figure 2: Incidence of shocks
**********************************
use LenderClassVars.dta, clear
		
		$G  org_tier1_cap_ratio org_MSR_ratio Delta_CR_2015_2008 GSE_nloan_rank Penalties_08_15  if year==2008  ,$S format(%10.3f)
				tab LenderTypeBroad year
			global CLUB LenderTypeBroad
				
		gen log10_Assets = log10(Assets_tb_Bn*1000000000)
			drop if  log10_Assets < 7 // less that 10 million
					
		* Capital			
				gen org_tier1_cap_ratio_pps = 100*org_tier1_cap_ratio
				gen Delta_CR_2015_2008_pps = 100*Delta_CR_2015_2008
			 graph bar (mean) org_tier1_cap_ratio_pps Delta_CR_2015_2008_pps if org_tier1_cap_ratio < 1 & inlist(year,2008),  ///
				over($CLUB  , label(labsize(large)))   ytitle(Percent,size(medlarge)  )  ylabel(, labsize(large))  title("") legend(pos(6) row(1)) name(Capital_mean_out, replace) ///
				legend(label(1 "2008 Capital Ratio") label(2 "2008-2015 Change in Capital Ratio") size(medlarge))

		* MSR
				gen org_MSR_ratio_pps = 100*org_MSR_ratio
				graph bar (mean) org_MSR_ratio_pps if org_tier1_cap_ratio < 1 & inlist(year,2008),  ///
					over($CLUB , label(labsize(large))) title("") legend(pos(6) row(1)) name(MSR_out, replace) ///
					ytitle("")  ytitle(Percent,size(medlarge)  )  ylabel(, labsize(large)) 

		* Penalties_08_15
			replace Penalties_08_15=0 if mi(Penalties_08_15)
			gen Penalties_Assets_percent = 100*(Penalties_08_15/1000) / Assets_tb_Bn
			gen Penalties_08_15_Bn = Penalties_08_15/1000
			graph bar (sum) Penalties_08_15_Bn if inlist(year,2008) & inrange($CLUB ,1,7) ,  ///
				ylabel(0(25)125, labsize(large)) over($CLUB  , label(labsize(large))) ytitle(Penalties (\$ Bn),size(medlarge) ) legend(pos(6) row(1)) name(Penalties_out, replace)

			 graph bar (mean) Penalties_Assets_percent if inlist(year,2008) & inrange($CLUB ,1,7) , ///
				over($CLUB  , label(labsize(large)))  ylabel(, labsize(large))  ytitle(Penalty/Assets (%),size(medlarge)  ) legend(pos(6) row(1)) name(Penalties_Assets_out, replace)
	
******************************************************
* Figure 7: The Changing Distribution of GSE Lending
******************************************************
		replace log10_Assets=. if  log10_Assets < 7 | log10_Assets > 13 // less that 10 million
		gen log10_GSErank = log10(GSE_nloan_rank)
		local plottype kdensity 
		local bw bw(.30)  kernel(gaus)
		local wt [fweight=org_GSE_originator] 
		twoway ( `plottype'  log10_GSErank if year==2011 `wt',`bw' ) ///
				( `plottype'  log10_GSErank if year==2015 `wt',`bw' ///
					title("") xtitle(GSE Lender Rank) ytitle(Density) ///
					legend(pos(6) rows(1) label(1 "2011 Density") lab(2 "2015 Density") )  xlabel(0 "1" 1 "10" 2 "100" 3 "1000"  ) ///
					name(PDF_GSE_rank_1115,replace))
			

